﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using ConfExpert.Utility.Grid;
using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using Extensions;
using ConfExpert.Utility;
using DevExpress.XtraReports.UI;
using DevExpress.XtraEditors;
namespace Tailord
{
    public partial class Kumas : XForm
    {
        string t;
        public Kumas(string _t)
        {
            InitializeComponent();
            t = _t;
        }


        BindingSource bs;
        private void Kumas_Load(object sender, EventArgs e)
        {
            TAM.CustomerTableAdapter = new TailordDS.AppDSTableAdapters.CustomerTableAdapter();
            TAM.KumasTableAdapter = new TailordDS.AppDSTableAdapters.KumasTableAdapter();
            TAM.KaliteTableAdapter = new TailordDS.AppDSTableAdapters.KaliteTableAdapter();

            bbiSave.ItemClick += bbiSave_ItemClick;
            
            MainToolbar.Visible = true;
            this.Text = t;
            switch (t)
            {
                case "User":
                    ds.User.Columns.Add("Firma", typeof(string));
                    break;

                case "Kalite":
                    ds.Kalite.Columns.Add("Sarf", typeof(decimal), "2");
                    ds.Kalite.Columns.Add("KumasMaliyeti", typeof(decimal), "Sarf*BirimFiyat");
                    ds.Kalite.Columns.Add("Yuzde10", typeof(decimal), "KumasMaliyeti*0.1");
                    ds.Kalite.Columns.Add("Cmt", typeof(decimal), "32.50");
                    ds.Kalite.Columns.Add("HesapMaliyet", typeof(decimal), "KumasMaliyeti+Yuzde10+Cmt");
                    ds.Kalite.Columns.Add("Kar15", typeof(decimal), "HesapMaliyet*0.15");
                    ds.Kalite.Columns.Add("ExportFob", typeof(decimal), "HesapMaliyet+Kar15");


                    break;
            }

            GetDetails("Firma;Bayi;User;Customer;Kumas;Kalite;Kampanya", "GetData");
            bs = new BindingSource(ds, t);
            if (bs.Count > 0)
                InitGridView(gv, bs);

            switch (t)
            {
                case "User":
                    gv.Columns["Firma"].VisibleIndex = gv.Columns["BayiId"].VisibleIndex;
                    gv.Columns["BayiId"].ColumnEdit = riBayi;
                    gv.MakeColumnInvisible("LastLogin");
                    gv.MakeReadOnly();
                    break;

                case "Customer":
                    gv.Columns["UserId"].ColumnEdit = riUser;
                    FirmaEkle(gv);
                    break;

                case "Kumas":
                    gv.Columns["KaliteId"].ColumnEdit = riKalite;
                    gv.Columns["Lokasyon"].ColumnEdit = riLokasyon;
                    gv.MakeColumnInvisible("Scan", "Deleted","BirimAlisFiyat");
                    UreticiEkle(gv);
                    break;

                case "Kalite":
                    gv.FormatAsNumber(2, "BirimFiyat", "KumasMaliyeti", "Yuzde10", "Cmt", "HesapMaliyet", "Kar15", "ExportFob");
                    gv.MakeColumnInvisible("BirimFiyatDC");
                    gv.SetCaptions("BirimFiyat:Birim Fiyat EUR", "Cmt:CMT EUR", "Sarf: Sarf Mt.","GrMt2:gr/mt2", "SIKLIK:Sıklık", "CozguIpNo:Çözgü İp No", "AtkiIpNo:Atkı İp No", "Karisim:Karışım");
                    //gv.YanYanaGetirCoklu("Tarih", "Sarf", "KumasMaliyeti", "Yuzde10", "Cmt", "HesapMaliyet", "Kar15", "ExportFob");
                    break;

                case "Order":
                    DataSet dsOrder =AppSession.DataManager.ExecuteDataSet("GetOrdersForYP");
                    gv.Init(dsOrder.Tables[0]);

                    gv.Columns["nFirmaId"].ColumnEdit = GetCustomGLE(dsOrder.Tables[1], "Unvan", "nFirmaId");
                    gv.Columns["CustomerId"].ColumnEdit = riCustomer;
                    gv.Columns["UserId"].ColumnEdit = riUser;
                    gv.Columns["FirmaId"].ColumnEdit = riFirma;
                    gv.Columns["FaturaKime"].ColumnEdit = riFaturaKime;
                    gv.Columns["OdemeTuru"].ColumnEdit = riOdemeTuru;
                    gv.Columns["KampanyaId"].ColumnEdit = riKampanya;
                    gv.Columns["WhoDeleteIs"].ColumnEdit = riUser;
                    gv.SetCaptions("WhoDeleteIs:Silen","nFirmaId:NebimFirma");
                    gv.FormatAsNumber(2, "ToptanFiyat", "PerakendeFiyat","Segment");
                    gv.MakeReadOnly("Kampanya","OdemeTuru","FaturaKime","nFirmaId");
                    gv.MakeColumnInvisible("nStokFisiId");
                    break;
            }

            gv.RowCellStyle += gv_RowCellStyle;
            gv.AddSummary();
            //gv.OptionsBehavior.EditorShowMode = EditorShowMode.MouseDown;
            
            if(gv.OptionsBehavior.ReadOnly)
                bbiSave.Enabled = false;                
            else
                gv.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;


            if ("Kumas,Order,Customer".Contains(t))     //,User
            {
                GridManager gm = GridManager.GetManager(gv);
                if (AppSession.UserInfo.UserId == 113 || AppSession.UserInfo.UserId == 1 || AppSession.UserInfo.UserId == 79)
                {
                    gm.GridMenu.AddMenu("Sil", Sil, Properties.Resources.delete);
                    gm.GridMenu.AddMenu("Geri Al", GeriAl, Properties.Resources.miNew);
                }
                
                if (t=="Order")
                {
                    gm.GridMenu.AddMenu("Fatura yazdır", PrintInvoice,Properties.Resources.invoice_icon,true);
                    gm.GridMenu.AddMenu("İrsaliye yazdır", PrintShip, Properties.Resources.invoice_icon);
                    gm.GridMenu.AddMenu("Faturayı iptal et", FaturaIptalEt, Properties.Resources.delete);
                    gm.GridMenu.AddMenu("Fatura no ekle", AddInvoice, Properties.Resources.invoice_icon);
                    gm.GridMenu.AddMenu("Kumaş ihtiyacını görüntüle", ShowKumasIhtiyaci, Properties.Resources.miNew, true);
                    gm.GridMenu.AddMenu("Ekstra kumaş siparişi ver", ManuelFabricPO, Properties.Resources.miNew);
                    gm.GridMenu.AddMenu("Kumaş Sipariş Föyü", PrintKumasFoy, Properties.Resources.miPrint);
                    gm.GridMenu.AddMenu("Föy yazdır", PrintFoy, Properties.Resources.miPrint,true);
                    //gm.GridMenu.AddMenu("Nebime Aktar", NebimeAktar); 
                }
                gv.RowStyle += gv_RowStyle;
                bbiHideDeleted.Checked = true;
            }
            else
                bbiHideDeleted.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            foreach (GridColumn col in gv.Columns)
                if (col.FieldName.Contains("Tarih"))
                    gv.FormatAsDate(col.FieldName);

            gv.BestFitColumns();
        }

        private void ManuelFabricPO(object sender, EventArgs e)
        {
            if (gv.FocusedRowHandle > -1)
            {
                string KumasSiparisDurumu = gv.GetRowCellValue(gv.FocusedRowHandle, "KumasSiparisDurumu").ToStringNN();

                if (KumasSiparisDurumu != "Tamamı")
                {
                    XtraMessageBox.Show("Kumaşların tamamı sipariş edilmemiş föy için ekstra kumaş siparişi veremezsiniz !");
                    return;
                }

                int OrderId = gv.GetRowCellValue(gv.FocusedRowHandle, "OrderId").ToIntZero();
                ManuelKumasSiparisi ki = new ManuelKumasSiparisi(OrderId);
                DialogResult dr = ki.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    bbiSave.PerformClick();
                }

            }
        }

        private void AddInvoice(object sender, EventArgs e)
        { 
            if (gv.FocusedRowHandle > -1)
            {
                int orderId = gv.GetFocusedRowCellValue("OrderId").ToIntZero();
              
                 
                FaturaEkle fe = new FaturaEkle(orderId);
                DialogResult dr = fe.ShowDialog();
                if (dr == DialogResult.OK)
                {   
                    ExecQuery("AddFaturaNo", orderId, fe.FaturaNo, fe.FaturaTarihi); 
                    XtraMessageBox.Show("Bilgiler eklendi!");
                    bbiSave.PerformClick();
                }
            }
        }

        private void PrintInvoice(object sender, EventArgs e)
        {
            OzelIndirim = 0;
            SiparisFiyati = 0;
            IrsaliyeNo = 0;
            OrderId = 0;
            if (gv.FocusedRowHandle > -1)
            {
                int orderId = gv.GetFocusedRowCellValue("OrderId").ToIntZero();
                string Prova = gv.GetRowCellValue(gv.FocusedRowHandle, "Prova").ToStringNN();
                string FaturaDurumu = gv.GetRowCellValue(gv.FocusedRowHandle, "FaturaDurumu").ToStringNN();
                bool IsProva = Prova == "Prova" && FaturaDurumu == "Kesilmedi";

                decimal PerakendeFiyat = gv.GetFocusedRowCellValue("PerakendeFiyat").ToDecimalNN();
                decimal ToptanFiyat = gv.GetFocusedRowCellValue("ToptanFiyat").ToDecimalNN();
                decimal Indirim = gv.GetFocusedRowCellValue("Segment").ToDecimalNN();


                if (ToptanFiyat > 0)
                    SiparisFiyati = ToptanFiyat;
                else
                {
                    if (Indirim > 0)
                        SiparisFiyati = Decimal.Round(PerakendeFiyat * ((100 - Indirim) / 100), 2);
                    else
                        SiparisFiyati = PerakendeFiyat;
                }

                if (FaturaDurumu == "Kesildi")
                {
                    XtraMessageBox.Show("Bu siparişin faturası zaten kesilmiş !");
                    return;
                }

                string odemeTuru = gv.GetRowCellValue(gv.FocusedRowHandle, "OdemeTuru").ToStringNN();
                FaturaOlustur fo = new FaturaOlustur(odemeTuru, IsProva, SiparisFiyati.ToString());
                DialogResult dr = fo.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    IrsaliyeNo = fo.FaturaNo;
                    OrderId = orderId;
                    IsProvaForFatura = fo.IsProva;
                    OzelFiyat = fo.OzelFiyat;

                    DataSet ds = AppSession.DataManager.ExecuteDataSet("GetFaturaForTailord", orderId, fo.OdemeTur, IsProvaForFatura, OzelFiyat);

                    string Unvan = ds.Tables[0].Rows[0]["tcUnvan"].ToStringNN();
                    OzelIndirim = ds.Tables[0].Rows[0]["Iskonto"].ToDecimalNN();

                    if (Unvan == "" || Unvan == "SN. ")
                    {
                        XtraMessageBox.Show("Müşterinin ÜNVAN bilgisi girilmemiş. Lütfen müşterinin fatura bilgilerini kontrol edin !");
                        return;
                    }

                    XtraReport irs;
                    if (fo.FaturaTipi == 2)
                    {
                        irs = new xrFatura(ds);
                        irs.PrintingSystem.StartPrint += PrintingSystem_StartPrint;
                    }
                    else
                        irs = new Tailord.Reports.xrFatura2016(ds);

                    irs.ShowPreview();
                    irs.PrintingSystem.EndPrint += PrintingSystem_EndPrint;
                }

            }
        }

        int IrsaliyeNo, OrderId;
        bool IsProvaForFatura;
        decimal SiparisFiyati, OzelIndirim, OzelFiyat;
        void PrintShip(object sender, EventArgs e)
        {
            IrsaliyeNo = 0;
            OrderId = 0;
            if (gv.FocusedRowHandle > -1)
            {
                int orderId = gv.GetFocusedRowCellValue("OrderId").ToIntZero();
                string Prova = gv.GetRowCellValue(gv.FocusedRowHandle, "Prova").ToStringNN();
                string IrsaliyeDurumu = gv.GetRowCellValue(gv.FocusedRowHandle, "IrsaliyeDurumu").ToStringNN();
                bool IsProva = Prova == "Prova" && IrsaliyeDurumu == "Kesilmedi";
                
                if (IrsaliyeDurumu == "Kesildi")
                {
                    XtraMessageBox.Show("Bu siparişin irsaliyesi zaten kesilmiş !");
                    return;
                }

                IrsaliyeOlustur io = new IrsaliyeOlustur( IsProva);
                DialogResult dr = io.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    IrsaliyeNo = io.IrsaliyeNo;
                    OrderId = orderId;
                    IsProvaForFatura = io.IsProva;

                    DataSet ds = AppSession.DataManager.ExecuteDataSet("GetIrsaliyeForTailord", orderId,  IsProvaForFatura);

                    string Unvan = ds.Tables[0].Rows[0]["tcUnvan"].ToStringNN();

                    if (Unvan == "" || Unvan == "SN. ")
                    {
                        XtraMessageBox.Show("Müşterinin ÜNVAN bilgisi girilmemiş. Lütfen müşterinin fatura bilgilerini kontrol edin !");
                        return;
                    }

                    xrIrsaliye irs = new xrIrsaliye(ds);
                    irs.PrintingSystem.ShowMarginsWarning = false;
                    irs.ShowPreview();
                    irs.PrintingSystem.StartPrint += PrintingSystem_StartPrint;
                    irs.PrintingSystem.EndPrint +=PrintingSystem_EndPrint2;
                }
            }
        }

        void PrintingSystem_EndPrint2(object sender, EventArgs e)
        {
            ExecQuery("UpdIrsaliyeKesildi", OrderId, IrsaliyeNo, IsProvaForFatura);
            Kumas.Notify("Done");
            bbiSave.PerformClick();
        }

        void PrintingSystem_EndPrint(object sender, EventArgs e)
        {
            ExecQuery("UpdFaturaKesildi", OrderId, IrsaliyeNo, IsProvaForFatura, OzelFiyat, OzelIndirim);
            Kumas.Notify("Done");
            bbiSave.PerformClick();
        }

        void PrintingSystem_StartPrint(object sender, DevExpress.XtraPrinting.PrintDocumentEventArgs e)
        {
            e.PrintDocument.PrinterSettings.Copies = 4;
            e.PrintDocument.PrinterSettings.Collate = false;
        }

        private void FaturaIptalEt(object sender, EventArgs e)
        {
            if (gv.FocusedRowHandle>-1)
            {
                string OrderId = gv.GetFocusedRowCellValue("OrderId").ToString();
                if (XtraMessageBox.Show(OrderId+ " numaralı föyün faturasını iptal etmek istiyor musunuz ?", "Critical", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                        return;
                ExecQuery("DelFatura", OrderId.ToIntZero());
                bbiSave.PerformClick();
            }
        }

        private void NebimeAktar(object sender, EventArgs e)
        {
            if (gv.FocusedRowHandle > -1)
            {
                string OrderIds = gv.GetColumnValues("OrderId");
                
                bbiSave.PerformClick();

                int RowCount = gv.DataTable().Select(string.Format("OrderId in ({0})", OrderIds)).Length;
                int SentRowCount = gv.DataTable().Select(string.Format("OrderId in ({0}) and nStokFisiId is not null", OrderIds)).Length;
                int FirmasizRowCount = gv.DataTable().Select(string.Format("OrderId in ({0}) and nFirmaId is null", OrderIds)).Length;
                int FaturasizRowCount = gv.DataTable().Select(string.Format("OrderId in ({0}) and FaturaTarihi is null", OrderIds)).Length;

                if (SentRowCount > 0)
                    XtraMessageBox.Show(string.Format("Seçilen faturaların {0} tanesi zaten Nebim'e aktarılmış. Lütfen Nebime aktarılmamış faturaları seçin.", SentRowCount));
                else if (FirmasizRowCount > 0)
                    XtraMessageBox.Show(string.Format("Seçilen faturaların {0} tanesinde cari seçili değil. Lütfen 'Nebim Firma' alanlarını doldurun.", FirmasizRowCount));
                else if (FaturasizRowCount > 0)
                    XtraMessageBox.Show(string.Format("Seçilen faturaların {0} tanesinin faturası kesilmemiş. Lütfen faturayı kestikten sonra Nebime aktarın.", FaturasizRowCount));
                else
                {
                    foreach (string orderId in OrderIds.Split(','))
                    {
                        int OrderId = orderId.ToIntZero();
                        AppSession.DataManager.ExecuteNonQuery("NebimeGonder", OrderId);
                    }

                    bbiSave.PerformClick();
                }
            }
        }

        private void PrintKumasFoy(object sender, EventArgs e)
        {
            if (gv.FocusedRowHandle > -1)
            {
                int UserId = AppSession.UserInfo.UserId;
                int OrderId = gv.GetRowCellValue(gv.FocusedRowHandle, "OrderId").ToIntZero();
                DataSet dsFoy = AppSession.DataManager.ExecuteDataSet("GetKumasSiparisFoy", OrderId, UserId, "", "", false);
                Reports.KumasSiparisFoyu ks = new Reports.KumasSiparisFoyu(dsFoy);
                ks.ShowPreview();
            }
        }

        private void ShowKumasIhtiyaci(object sender, EventArgs e)
        {
            if (gv.FocusedRowHandle > -1)
            {
                int OrderId = gv.GetRowCellValue(gv.FocusedRowHandle, "OrderId").ToIntZero();
                string Prova = gv.GetRowCellValue(gv.FocusedRowHandle, "Prova").ToStringNN();
                string KumasSiparisDurumu = gv.GetRowCellValue(gv.FocusedRowHandle, "KumasSiparisDurumu").ToStringNN();
                bool IsProva = Prova == "Prova" && KumasSiparisDurumu == "Verilmedi";
                KumasIhtiyaci ki = new KumasIhtiyaci(OrderId,IsProva);
                DialogResult dr= ki.ShowDialog();
                if (dr==DialogResult.OK)
                {
                    bbiSave.PerformClick();
                }
            }
        }

        private void PrintFoy(object sender, EventArgs e)
        {
            if (gv.FocusedRowHandle > -1)
            {
                int orderId = gv.GetFocusedRowCellValue("OrderId").ToIntZero();
                DataSet dse = AppSession.DataManager.ExecuteDataSet("GetOrdersforTailord", orderId, AppSession.UserInfo.UserId);
                SiparisFormu sf = new SiparisFormu(0, dse);
                sf.ShowPreview();
            }
        }
        
        void gv_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {            
            switch (e.Column.FieldName)
            {
                case "5":
                case "Name":
                case "KumasKodu":
                case "BirimFiyat":
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
                    break;
                case "FiyatFarki":
                    if (e.RowHandle > -1)
                    {
                        string message = gv.GetRowCellValue(e.RowHandle,"FiyatFarki").ToString();
                        if (message.ToLower().Contains("negatif"))
                            e.Appearance.BackColor = Color.RoyalBlue;
                        else if (message.ToLower().Contains("pozitif"))
                            e.Appearance.BackColor = Color.Red;
                    }
                    return;
                case "KumasSiparisDurumu":
                    if (e.RowHandle > -1)
                    {
                        string message = gv.GetRowCellValue(e.RowHandle, "KumasSiparisDurumu").ToString();
                        if (message.ToLower()=="verilmedi")
                            e.Appearance.BackColor = Color.Gainsboro;
                        else if (message.ToLower().Contains("kısmen"))
                            e.Appearance.BackColor = Color.LightGoldenrodYellow;
                        else if (message.ToLower().Contains("tamamı"))
                            e.Appearance.BackColor = Color.DarkSeaGreen;
                    }
                    return;
                case "FaturaDurumu":
                    if (e.RowHandle > -1)
                    {
                        string message = gv.GetRowCellValue(e.RowHandle, "FaturaDurumu").ToString();
                        if (message.ToLower() == "kesilmedi")
                            e.Appearance.BackColor = Color.Gainsboro;
                        else if (message.ToLower().Contains("yalnızca prova"))
                            e.Appearance.BackColor = Color.LightGoldenrodYellow;
                        else if (message.ToLower().Contains("kesildi"))
                            e.Appearance.BackColor = Color.DarkSeaGreen;
                    }
                    return;
                case "IrsaliyeDurumu":
                    if (e.RowHandle > -1)
                    {
                        string message = gv.GetRowCellValue(e.RowHandle, "IrsaliyeDurumu").ToString();
                        if (message.ToLower() == "kesilmedi")
                            e.Appearance.BackColor = Color.Gainsboro;
                        else if (message.ToLower().Contains("yalnızca prova"))
                            e.Appearance.BackColor = Color.LightGoldenrodYellow;
                        else if (message.ToLower().Contains("kesildi"))
                            e.Appearance.BackColor = Color.DarkSeaGreen;
                    }
                    return;
            }
        }

        private void FirmaEkle(GridView gv)
        {
            GridColumn colFirma = new GridColumn();
            colFirma.FieldName = "FirmaId";
            gv.Columns.Add(colFirma);

            colFirma.UnboundType = DevExpress.Data.UnboundColumnType.Integer;
            gv.CustomUnboundColumnData += gv_CustomUnboundColumnData;
            gv.Columns["FirmaId"].ColumnEdit = riFirma;
            gv.Columns["FirmaId"].VisibleIndex = gv.Columns["UserId"].VisibleIndex + 1;
            gv.Columns["FirmaId"].OptionsColumn.AllowEdit = false;
            //gv.Columns["FirmaId"].FilterMode = DevExpress.XtraGrid.ColumnFilterMode.Value;
        }

        void UreticiEkle(GridView gv)
        {
            GridColumn col = new GridColumn();
            col.FieldName = "Uretici";
            gv.Columns.Add(col);

            col.UnboundType = DevExpress.Data.UnboundColumnType.String;
            gv.CustomUnboundColumnData += gv_CustomUnboundColumnData;
            gv.Columns["Uretici"].VisibleIndex = gv.Columns["KaliteId"].VisibleIndex + 1;
            gv.Columns["Uretici"].OptionsColumn.AllowEdit = false;
            //gv.Columns["Uretici"].FilterMode = DevExpress.XtraGrid.ColumnFilterMode.Value;
        }

        public void gv_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.ListSourceRowIndex > -1)
                if (e.IsGetData)
                    if (e.Column.FieldName == "FirmaId")
                    {
                        int UserId = (((DataRowView)e.Row)["UserId"]).ToIntNN();
                        if (UserId > 0)
                        {
                            TailordDS.AppDS.UserRow ur = ds.User.FindByUserId(UserId);

                            if (ur != null)
                                if (!ur.IsBayiIdNull())
                                {
                                    int BayiId = ur["BayiId"].ToIntNN();
                                    if (BayiId > 0)
                                    {
                                        TailordDS.AppDS.BayiRow br = ds.Bayi.FindByBayiId(BayiId);

                                        if (br != null)
                                            if (!br.IsFirmaIdNull())
                                                e.Value = br.FirmaId;
                                    }
                                }
                        }
                    }
                    else if (e.Column.FieldName == "Uretici")
                    {
                        int KaliteId = (((DataRowView)e.Row)["KaliteId"]).ToIntNN();
                        if (KaliteId > 0)
                        {
                            TailordDS.AppDS.KaliteRow kr = ds.Kalite.FindByKaliteId(KaliteId);

                            if (kr != null)
                                if (!kr.IsUreticiNull())
                                    e.Value = kr.Uretici;

                        }
                    }


        }


        public RepositoryItemGridLookUpEdit riFirma
        {
            get
            {
                return GetCustomGLE(ds.Firma, "Ad", "FirmaId");
            }
        }

        public RepositoryItemGridLookUpEdit riBayi
        {
            get
            {
                RepositoryItemGridLookUpEdit ri = GetCustomGLE(ds.Bayi, "Ad", "BayiId");
                ri.View.Columns["FirmaId"].ColumnEdit = riFirma;
                return ri;
            }
        }

        public RepositoryItemGridLookUpEdit riKampanya
        {
            get
            {
                RepositoryItemGridLookUpEdit ri = GetCustomGLE(ds.Kampanya, "Name", "KampanyaId");
                return ri;
            }
        }

        public RepositoryItemGridLookUpEdit riUser
        {
            get
            {
                RepositoryItemGridLookUpEdit ri = GetCustomGLE(ds.User, "Name", "UserId");
                ri.View.Columns["BayiId"].ColumnEdit = riBayi;
                return ri;
            }
        }

        public RepositoryItemGridLookUpEdit riCustomer
        {
            get
            {
                RepositoryItemGridLookUpEdit ri = GetCustomGLE(ds.Customer, "Name", "CustomerId");
                ri.View.Columns["UserId"].ColumnEdit = riUser;
                return ri;
            }
        }

        public RepositoryItemGridLookUpEdit riFaturaKime
        {
            get
            {
                RepositoryItemGridLookUpEdit ri = GetCustomRI("FaturaKime", false, "Şahıs", "Firma");  
                return ri;
            }
        }

        public RepositoryItemGridLookUpEdit riOdemeTuru
        {
            get
            {
                RepositoryItemGridLookUpEdit ri = GetCustomRI("OdemeTuru", false, "Belli Değil", "Kredi Kartı", "EFT", "Nakit");
                return ri;
            }
        }

        public RepositoryItemGridLookUpEdit riKalite
        {
            get
            {
                return GetCustomGLE(ds.Kalite, "Name", "KaliteId");
            }
        }
        
        public RepositoryItem riLokasyon
        {
            get
            {
                RepositoryItemGridLookUpEdit gleTedarikYeri = GetCustomRI("TedarikYeri", false, "SÖKTAŞ", "TAILORD", "AKFİL", "DESTEX", "ALBINI");
                return gleTedarikYeri;
            }
        }

        public override void bbiHideDeleted_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gv.Columns.Contains(gv.Columns[del]))
                bs.Filter = bbiHideDeleted.Checked ? "ISNULL(" + del + ",0)=0" : string.Empty;
        }

        string del = "Deleted";

        void Sil(object sender, EventArgs e)
        {
            string str = string.Empty;
            foreach (int rowHandle in gv.GetSelectedRows())
            {
                gv.SetRowCellValue(rowHandle, del, true);
                gv.SetRowCellValue(rowHandle, "WhoDeleteIs", AppSession.UserInfo.UserId);
            }
            bbiSave.PerformClick();
        }

        void GeriAl(object sender, EventArgs e)
        {
            string str = string.Empty;
            foreach (int rowHandle in gv.GetSelectedRows())
                gv.SetRowCellValue(rowHandle, del, false);
            bbiSave.PerformClick();
        }

        void gv_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {

            if (e.RowHandle>-1)
            {
                int currentStGenelId = gv.GetRowCellValue(e.RowHandle, del).ToIntZero();

                if (currentStGenelId == 1)
                {
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Strikeout);
                    e.Appearance.ForeColor = Color.Gray;
                }
            }
            //if (gv.GetRowCellValue(e.RowHandle, del) != null)
            //    if (gv.GetRowCellValue(e.RowHandle, del) != DBNull.Value)
            //        if ((bool)(gv.GetRowCellValue(e.RowHandle, del)))
            //            e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Strikeout);

        }

        public  void bbiSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gv.CloseEditor();
            gv.UpdateCurrentRow();
            bs.EndEdit();

            switch (t)
            {
                case "Order":
                    if (gv.Dataset().HasChanges())
                        gv.Update("OrderId,KampanyaId,OdemeTuru,FaturaKime,Deleted,WhoDeleteIs,nFirmaId", "[Order]");
                    int focusedRow = gv.FocusedRowHandle;
                    gv.UpdateData("GetOrdersForYP");
                    if (focusedRow + 1 >= gv.RowCount)
                    {
                        gv.FocusedRowHandle = gv.RowCount - 2;
                    }
                    else
                    {
                        gv.FocusedRowHandle = focusedRow;
                    }
                    break;

                default:
                    Save();
                    break;
            }
            //AppSession.DataManager.ExecuteNonQuery("COInsLog", "Style", style);
            Notify("Saved successfully.");
        }
        
    }
}
